import requests
from bs4 import BeautifulSoup
from PIL import Image,ImageTk
import tkinter as tk
import io
import pickle
from tkinter import messagebox
index = -1
h = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
r = requests.get('https://www.xs8.cn/finish',headers=h)
c = r.content
result_list = []
soup = BeautifulSoup(c,'html.parser')
Pagenum = soup.find('div',{'class':'right-book-list'}).find_all('li')[-2].text
for i in range(1,int(10)+1):    
    url = 'https://www.xs8.cn/all?pageSize=10&gender=2&catId=-1&isFinish=-1&isVip=-1&size=-1&updT=-1&orderBy=0&pageNum=' + str(i)
    p_r = requests.get(url,headers=h)
    p_c = p_r.content
    p_soup = BeautifulSoup(p_c,'html.parser')
    all_img_list = p_soup.find('div',{'class':'right-book-list'}).find_all('li')
    for item in all_img_list:
        d = {}
        d['img'] = item.find('div',{'class':'book-img'}).find('img')['src']
        d['title'] = item.find('div',{'class':'book-info'}).find('h3').find('a').text
        d['intro'] = item.find('div',{'class':'book-info'}).find('p',{'class':'intro'}).text
       
        result_list.append(d)

def showInfo(i):
    item = result_list[i]
    url = 'http:' + item['img']
    img_r = requests.get(url,headers=h)
    img_c = img_r.content
    data_stream = io.BytesIO(img_c)
    img = Image.open(data_stream)
    photo = ImageTk.PhotoImage(img)
    imgLable.config(image=photo)
    imgLable.image=photo
    titleLable.config(text=item['title'])
    introLabel.config(text=item['intro'])

def nextpic():#下一个
    global index
    index += 1
    showInfo(index)

def shagn():#上一个
    global index
    index -=1
    showInfo(index)

def login():
    name = nameValue.get()
    password = passwordValue.get()
    try:
        with open('usrInfo.pickle','rb') as usr_file:
            usrInfo = pickle.load(usr_file)
    except Exception as e:
        with open('usrInfo.pickle','wb') as usr_file:
            usrInfo = {'admin':'admin'}
            pickle.dump(usrInfo,usr_file)
    if name in usrInfo:
        if usrInfo[name] == password:
            tk.messagebox.showinfo(title='欢迎!',message='%s 用户欢迎你！'% name)
        else:
            tk.messagebox.showerror(title='错误!',message='密码错误请重试!')
    else:
        yn = tk.messagebox.askyesno(title='错误!',message='用户不存在，您是否需要注册?')
        if yn:
            signUp()

def signUp():
    def ok():
        name = signUp_nameValue.get()
        passwd = signUp_passwordValue.get()
        confirmPasswd = confirm_passwordValue.get()

        try:
            with open('usrInfo.packle','rb') as usr_file:
                usrInfo = pickle.load(usr_file)  
        except Exception as e:
            with open('usrInfo.pickle','wb') as usr_file:
                usrInfo = {'admin':'admin'}
                pickle.dump(usrInfo,usr_file)
        if name != '':                #判断用户名是否为空
            if name in usrInfo:
                tk.messagebox.showinfo(title='提示',message='%s 此用户已经存在'% name)
            else:
                if passwd != '' and passwd == confirmPasswd:
                    usrInfo[name] = passwd
                    print(usrInfo)
                    with open('usrInfo.pickle','wb') as usr_file:
                        pickle.dump(usrInfo,usr_file)
                    tk.messagebox.showinfo(title='恭喜您加入言情小说吧',message='%s 注册成功!' % name)
                    signUp_win.destroy()
                else:
                    tk.messagebox.showerror(title='错误',message='密码不能为或者确认密空')
        else:
            tk.messagebox.showerror(title='错误',message='用户名不能为空')
    def cancel():
        signUp_win.destroy()


    signUp_win = tk.Toplevel(window)
    signUp_win.title('注册')
    signUp_win.geometry('300x200')
    
    tk.Label(signUp_win,text='用户名:').place(x=30,y=20)
    signUp_nameValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=signUp_nameValue).place(x=100,y=20)

    tk.Label(signUp_win,text='密  码:').place(x=30,y=60)
    signUp_passwordValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=signUp_passwordValue,show='*').place(x=100,y=60)

    tk.Label(signUp_win,text='确认密码;').place(x=30,y=100)
    confirm_passwordValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=confirm_passwordValue,show='*').place(x=100,y=100)

    tk.Button(signUp_win,text='确定',command=ok,bg='#EED5D2').place(x=110,y=140)
    tk.Button(signUp_win,text='取消',command=cancel,bg='#EED5D').place(x=160,y=140)
    #登录界面显示框
window = tk.Tk()
window.title('欢迎进入言情小说吧')
window.geometry('400x300')

canvas = tk.Canvas(window,width=400,height=300,bg='pink')#创建画布
img = Image.open('1.jpg')#背景照片
img = img.resize((800,700),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image=img)#加载文件图片
canvas.create_image(0,0,anchor='nw',image=photo)
canvas.place(x=0,y=0)

nameLbl = tk.Label(window,text='用户名:')
nameLbl.place(x=30,y=150)

nameValue = tk.StringVar()
tk.Entry(window,textvariable=nameValue).place(x=100,y=150)

tk.Label(window,text='密 码:').place(x=30,y=200)
passwordValue = tk.StringVar()
tk.Entry(window,textvariable=passwordValue,show='*').place(x=100,y=200)

tk.Button(window,text='登录',command=login,bg='#EED5D2').place(x=120,y=240)
tk.Button(window,text='注册',command=signUp,bg='#EED5D2').place(x=180,y=240)

window.mainloop()

window = tk.Tk()

window.title('言情小说')
window.geometry('500x400')

imgLable = tk.Label(window,bg='gray')
imgLable.pack()

titleLable = tk.Label(window,bg='#EEB4B4')
titleLable.pack()

introLabel = tk.Label(window,bg='pink',height=3)
introLabel.pack()


tk.Button(window,text='上一页',command=shagn,bg='#CAE1FF').place(x=100,y=300)
tk.Button(window,text='下一页',command=nextpic,bg='#FF6A6A').place(x=300,y=300)

showInfo(index)



window.mainloop()
